package cn.edu.hziee;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@WebFilter(filterName = "CharacterFilter",urlPatterns = "/*")
public class CharacterFilter implements Filter {

    public void init(FilterConfig config) throws ServletException {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        CharacterRequest characterRequest = new CharacterRequest(request);
        chain.doFilter(characterRequest, response);
    }

    public void destroy() {
    }

}

class CharacterRequest extends HttpServletRequestWrapper {
    private HttpServletRequest request;
    public CharacterRequest(HttpServletRequest request){
        super(request);
        this.request = request;
    }

    public String getParameter(String name){
        String value = super.getParameter(name);
        if(value == null){
            return null;
        }
        String method = super.getMethod();
        if("get".equalsIgnoreCase(method)){
            try{
                value = new String(value.getBytes("iso-8859-1"),"utf-8");
            }catch (UnsupportedEncodingException e){
                throw new RuntimeException(e);
            }
        }
        return value;
    }
}
